home *** CD-ROM | disk | FTP | other *** search
- /*
- ------------------------------------------------------------------------------
- | Sun Microsystems, TOPS Division
- | 950 Marina Village Parkway
- | P.O. Box 4016
- | Alameda, CA 94501
- |
- | Copyright (c) 1989 Sun Microsystems, Inc. All rights reserved.
- |
- | Sun considers its source code as an unpublished, proprietary trade secret,
- | and it is available only under strict license provisions. This copyright
- | notice is placed here only to protect Sun in the event the source is deemed
- | a published work. Disassembly, decompilation, or other means of reducing the
- | object code to human readable form is prohibited by the license agreement
- | under which this code is provided to the user or company in possession of
- | this copy.
- |
- | RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the Government
- | is subject to restrictions as set forth in subparagraph (c) (1) (ii) of the
- | Rights in Technical Data and Computer Software clause at DFARS 52.227-7013
- | and in similar clauses in the FAR and NASA FAR supplement.
- ------------------------------------------------------------------------------
- */
-
-
-
- /*
- ================================================================================
- **
- ** Project: SoftTalk
- **
- ** File: STClient.c
- **
- ** Purpose:
- ** This file implements the sample SoftTalk client. It demonstrates how to
- ** look up a server, how to connect to a server, and how to make RPC calls
- ** to the server.
- **
- ** ----------------------------------------------------------------------------
- **
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 1.0.1 17-Aug-89 MAC First draft.
- **
- ================================================================================
- */
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** INCLUDES
- ** ----------------------------------------------------------------------------
- */
- #include "STClient.h"
- #include <MacTypes.h>
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** CONST
- ** ----------------------------------------------------------------------------
- */
-
- #define kListLoopCount (5)
- /* how many times to loop calling STList() */
-
- #define kNoClient (0)
- /* we don't have a client yet */
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** VAR
- ** ----------------------------------------------------------------------------
- */
- STSession gClientID = kNoClient;
- /* the SoftTalk magic cookie for our client. Initialized to kNoClient so it
- represents an invalid value. Only after calling STCreateClient() will
- this value be valid */
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** PROTOTYPES
- ** ----------------------------------------------------------------------------
- */
- static INT32 _MyNotify(void);
- static INT32 MyNotify(STSession session, INT32 why, Func32Ptr f, INT32 arg);
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION MyNotify() => INT32 : do we abort the session?
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** This interrupt-level routine is called from SoftTalk when there are problems
- ** reaching the server. We decide whether to abort the session, and can take
- ** appropriate action to clean-up our state. This sample always aborts and
- ** takes no clean-up actions.
- **
- ** Usage:
- ** When we create a session we tell SoftTalk to use this as the Notify function
- ** for the session. Since SoftTalk passes args in registers, we have some asm
- ** glue that pushes the args onto the stack and then executes a standard C function
- ** call.
- ** This asm glue routine is registered with SoftTalk, and will then be called by
- ** SoftTalk (at interrupt time) whenever the session is in trouble.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- static INT32 _MyNotify(void) {
- asm {
- move.l d3,-(sp) /* move "INT32 arg" onto the stack */
- move.l d2,-(sp) /* move "Func32Ptr F" onto the stack */
- move.l d1,-(sp) /* move "INT32 why" onto the stack */
- move.l d0,-(sp) /* move "STSession session" onto the stack */
- jsr MyNotify /* call the C routine */
- add.l #16,sp /* clean up the stack */
- }
- }
-
- static INT32 MyNotify(STSession session, INT32 why, Func32Ptr F, INT32 arg) {
- return((INT32)TRUE);
- }
-
-
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION STClientCreate() => INT32 : initialize the sample Client
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** Initialize the client by: initialize SoftTalk using STInitialize();
- ** find all servers that match our server's description using STList();
- ** open a session to our server using STOpenSession() (I cheat since I know the
- ** exact name of my server); clean-up by calling STUnlist().
- **
- ** Usage:
- ** after creating a client object with the new operator, immediately call
- ** this method in order to initialize the client.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- INT32 STClientCreate(void) {
-
- short i; /* loop counter */
- STSession myClient; /* the session "magic cookie" returned by SoftTalk */
- STResult err; /* result code */
- STServerSpecPtr pElt; /* ptr to a Server Spec that will be created by STLIst */
- BYTE* searchSpec; /* NBP spec for servers I want to match */
- BYTE* nameSpec; /* actual NBP spec for my server */
-
-
- /* do we have a client already? If yes then simply return */
- if (gClientID != kNoClient) {
- return(noErr);
- }
-
-
- /* initialize the NBP spec to find the appropriate servers. My server is
- of type "Dude", so I look for "=:Dude@*" which means "find all servers of
- type Dude in the current zone. For more info, see the description of
- NBP names in Inside Mac */
- searchSpec = (BYTE*)"\p=:Dude@*";
- nameSpec = (BYTE*)"\pMichael:Dude@*";
-
-
- /* initialize SoftTalk */
- err = STInitialize();
- if (err != noErr) {
- return(err);
- }
-
-
- /* SoftTalk has been initialized. We now
- list all servers that match what I'm looking for.
- If STList starts an async lookup, it will return kStillLooking; I need to then
- call STList again to actually get some values */
- err = STList(&pElt, searchSpec);
-
- if (err == kStillLooking) { /* started async lookup so call it again a couple of times */
- for(i = 0; (i < kListLoopCount) && ( (err == noErr) || (err == kStillLooking) ); i++) {
- err = STList(&pElt, searchSpec);
- }
- }
-
-
- /* check errors for STList */
- if (err < noErr) {
- return(err); /* SoftTalk error */
- }
- else if (err == 0) {
- return(kENoServerFound); /* no servers matched my spec */
- }
-
-
- /* we found some servers! Let's open a session to one of them.
- I already know the name of the sample server, so instead of
- extracting the name from the list returned by STList, I call STOpenSession
- with my predefined name. You should really extract the names and choose one
- of them */
- err = STOpenSession(&myClient, nameSpec, _MyNotify, kDefaultRequests);
- gClientID = myClient;
-
- if (err > noErr) {
- /* deallocate the list created by SoftTalk */
- err = STUnlist(searchSpec);
- }
- else if (err == 0) {
- /* no servers found */
- err = kENoServerFound;
- }
-
- return(err);
- }
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION STClientDispose() => STResult : dispose of the client
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** clean-up the client when you're done with it by closing the session
- ** using STCloseSession().
- **
- ** Usage:
- ** call this when you're done with the SoftTalk client.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- STResult STClientDispose(void){
- STResult result;
-
- if (gClientID != kNoClient) {
- result = STCloseSession(gClientID);
- gClientID = kNoClient;
- }
- else {
- result = noErr;
- }
- return(result);
- }
-
-
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION Func1() => INT32 : sample stub routine
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** This is a sample stub for an RPC call to the server with no args.
- **
- ** Usage:
- ** call this function when you want to invoke an RPC call.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- INT32 Func1(void){
- STResult err;
- INT32 result = (-1); /* initialize return value to failure */
- BYTE* format = (BYTE*)""; /* the string specifying the format of the arguments */
-
- err = STAsk(gClientID, &result, (UINT32)'F1', format);
- return(result);
- }
-
-
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION Func2() => INT32 : sample stub routine
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** This is a sample stub for an RPC call to the server with one INT32 arg.
- **
- ** Usage:
- ** call this function when you want to invoke an RPC call.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- INT32 Func2(INT32 arg1){
- STResult err;
- INT32 result = (-1); /* initialize return value to failure */
- BYTE* format = (BYTE*)"4"; /* the string specifying the format of the arguments */
-
- err = STAsk(gClientID, &result, (UINT32)'F2', format, (STArg) arg1);
- /* cast each argument to an STArg!!! ^ */
- return(result);
- }
-
-
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION Func3() => INT32 : sample stub routine
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** This is a sample stub for an RPC call to the server with two args.
- **
- ** Usage:
- ** call this function when you want to invoke an RPC call.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- INT32 Func3(INT32 arg1, INT16 arg2){
- STResult err;
- INT32 result = (-1); /* initialize return value to failure */
- BYTE* format = (BYTE*)"42"; /* the string specifying the format of the arguments */
-
- err = STAsk(gClientID, &result, (UINT32)'F3', format, (STArg) arg1, (STArg) arg2);
- /* cast each argument to an STArg!!! ^ ^ */
- return(result);
- }
-
-
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION Func4() => INT32 : sample stub routine
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** This is a sample stub for an RPC call to the server with one C-string arg.
- **
- ** Usage:
- ** call this function when you want to invoke an RPC call.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- INT32 Func4(char* arg1){
- STResult err;
- INT32 result = (-1); /* initialize return value to failure */
- BYTE* format = (BYTE*)"C"; /* the string specifying the format of the arguments */
-
- err = STAsk(gClientID, &result, (UINT32)'F4', format, (STArg) arg1);
- /* cast each argument to an STArg!!! ^ */
- return(result);
- }
-
-
-
-
-
- /*
- ** ----------------------------------------------------------------------------
- ** FUNCTION Func5() => INT32 : sample stub routine
- ** ----------------------------------------------------------------------------
- **
- ** Purpose:
- ** This is a sample stub for an RPC call to the server with one P-string arg.
- **
- ** Usage:
- ** call this function when you want to invoke an RPC call.
- **
- ** ----------------------------------------------------------------------------
- ** Version Date Author Description
- ** ------- ---- ------ -----------
- ** 01a 8/17/89 MAC First draft.
- ** ----------------------------------------------------------------------------
- */
-
- INT32 Func5(Str255 pString){
- STResult err;
- INT32 result = (-1); /* initialize return value to failure */
- BYTE* format = (BYTE*)"P"; /* the string specifying the format of the arguments */
-
- err = STAsk(gClientID, &result, (UINT32)'F5', format, (STArg) pString);
- /* cast each argument to an STArg!!! ^ */
- return(result);
- }
-
-